<template>
	<view class="search">
		<view class="title">
			<u-icon name="arrow-left" size="24" @click="back"></u-icon>
			<text style="margin-left: 24rpx;">搜索</text>
		</view>
		<view class="searchbox">
			<view class="searchboxitem">
				<u--input placeholder="请输入姓名搜索" v-model="searchValue" prefixIcon="search"
					prefixIconStyle="font-size: 22px;color: #909399" shape="circle" @input="handleSearch"></u--input>
				<text class="searchtext" @click="getTeacherLists"> 搜索</text>
			</view>
			<view class="quxiao" @click="handleQuxiao">
				取消
			</view>
		</view>
		<view v-if="show" class="historydata">
			<view class="historytitle">
				<view class="text">
					搜索历史
				</view>
				<view class="del" @click="handleDelHis">
					<image class="deleteimg" src="http://sssx.nanjingyunpeng.cn/uploads/tupian/image/delete.png">
					</image>
				</view>
			</view>
			<view class="historydatacontent">
				<view class="hisItem" v-for="(item,index) in historySearchData" @click="handleHistory(item)">
					{{item}}
				</view>
			</view>
		</view>
		<view v-else class="searchContent">
			<view class="searchtabs">
				<scroll-view class="scroll-view_Hs" scroll-x="true" scroll-left="0">
					<view class="itemone" v-for="(item,index) in tabsData" :key="item.id">
						{{item.name}}
					</view>
				</scroll-view>
			</view>
			<view class="searchcontentData">
				<view class="searchcontentDataitem" v-for="(item,index) in tearchData" :key="item.id"
					@click="gotoTeacherdetail(item.id)">
					<view class="imgbox">
						<image class="tearchtimg" :src="item.photos.url"></image>
					</view>
					<view class="messagebox">

						<view class="name">
							{{item.name}}
						</view>
						<view class="zhicheng">
							{{item.school}}
						</view>
						<!-- <view class="zhuanye">
							<view class="zhichengitem" v-for="(subitem,subindex) in item.science_ids_text"
								:key="subindex">
								{{subitem}}
							</view>
						</view> -->
						<scroll-view class="zhuanye" scroll-x="true" >
							<view class="zhichengitem" v-for="(subitem,subindex) in item.science_ids_text"
								:key="subindex">
								{{subitem}}
							</view>
						</scroll-view>
					</view>
				</view>

			</view>
		</view>

	</view>
</template>

<script>
	import {
		getpersonal,
		getTeacherList
	} from "@/api/api.js"
	export default {
		data() {
			return {
				searchValue: "",
				show: true,
				tabsData: [],
				tearchData: [],
				historySearchData: [],
			}
		},
		onLoad() {
			this.getpersonals(),
				this.historySearchData = JSON.parse(uni.getStorageSync("historySearch"))
			console.log("this.historySearchData", this.historySearchData)
		},
		methods: {
			async getpersonals() {
				let res = await getpersonal()
				if (res.code == 1) {
					console.log("学生信息", res)
					this.tabsData.push({
						name: res.data.science_id_text
					})
					console.log(this.tabsData)

				}
			},
			handleSearch(e) {
				console.log("搜索内容", e)
				if (e == '') {
					this.show = true
				} else {
					// this.getTeacherLists()
				}
			},
			handleHistory(item) {
				this.searchValue = item
				this.getTeacherLists()
			},
			async getTeacherLists() {
				console.log("历史搜索", uni.getStorageSync("historySearch"))
				// return
				if (this.searchValue == '') {
					uni.showToast({
						title: '请输入搜索内容！',
						icon: "none",
						duration: 1000
					});
					return
				}
				
				let res = await getTeacherList({
					name: this.searchValue,
					page: 1,
					size: 20
				})
				if (uni.getStorageSync("historySearch")) {
					let data = JSON.parse(uni.getStorageSync("historySearch"))
					let flag = true
					data.forEach((item,index)=>{
						console.log(item,this.searchValue)
						if(item == this.searchValue ){
							flag = false
						}
					})
					console.log("data", data)
					if(flag){
						data.push(this.searchValue)
						uni.setStorageSync("historySearch", JSON.stringify(data))
					}
					
				} else {
					let dataArr = [this.searchValue]
					uni.setStorageSync("historySearch", JSON.stringify(dataArr))
				}
				this.historySearchData = JSON.parse(uni.getStorageSync("historySearch"))
				if (res.code == 1) {
					console.log("搜索内容", res)
					this.tearchData = res.data
					if (this.tearchData.length > 0) {
						this.show = false

					}
					this.tearchData.forEach((item, index) => {
						item.science_ids_text = item.science_ids_text.split(",")
					})
				}
				if (res.data.length == 0) {
					uni.showToast({
						title: '暂无该老师，请重新搜索！',
						icon: 'none',
						duration: 1000
					})
				}
			},
			handleQuxiao() {
				this.back()
			},
			handleDelHis() {
				let that = this
				uni.showModal({
					// title: '提示',
					content: '确定要删除搜索历史吗？',
					confirmColor: '#4A9CFF',
					success: async function(res) {
						console.log("是否选择", res)
						if (res.confirm) {
							console.log("wwwww", uni.getStorageSync("historySearch"))
							uni.removeStorageSync("historySearch")
							that.historySearchData = []
							uni.showToast({
								title: '已删除历史搜索记录',
								icon: 'none',
								duration: 1000
							})

						}
					}
				})


			},
			gotoTeacherdetail(id) {
				uni.navigateTo({
					url: `../teacherDetail/index?id=${id}`,
				})
			},
			back() {
				uni.navigateBack({
					delta: 1
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.scroll-view_Hs {
		white-space: nowrap;
		width: 100%;
		border-radius: 20rpx 20rpx 20rpx 20rpx;
		height: 114rpx;
		padding: 20rpx;
	}

	.itemone {
		width: 172rpx;
		height: 60rpx;
		background: #4A9CFF;
		border-radius: 40rpx 40rpx 40rpx 40rpx;
		opacity: 1;
		font-size: 28rpx;
		font-family: PingFang SC-Regular, PingFang SC;
		font-weight: 400;
		color: #FFFFFF;
		line-height: 60rpx;
		text-align: center;

	}

	.historydatacontent {
		display: flex;
		margin-top: 10rpx;
		width: 100%;
		flex-wrap: wrap;

		.hisItem {
			// min-width: 180rpx;
			height: 68rpx;
			background: #F7F8FA;
			border-radius: 12rpx 12rpx 12rpx 12rpx;
			opacity: 1;
			font-size: 28rpx;
			font-family: PingFang SC-Regular, PingFang SC;
			font-weight: 400;
			color: #333333;
			line-height: 69rpx;
			text-align: center;
			margin-right: 10rpx;
			margin-bottom: 10rpx;
			padding: 0 10rpx;
		}
	}

	.search {
		min-height: 100%;

		.title {
			width: 100%;
			height: 176rpx;
			padding-top: 110rpx;
			box-sizing: border-box;
			// background-color: red;
			display: flex;
			align-items: center;
			font-size: 32rpx;
			font-family: PingFang SC-Bold, PingFang SC;
			font-weight: bold;
			color: #333333;
			line-height: 38rpx;
			padding-left: 34rpx;
			box-sizing: border-box;

		}

		.searchbox {
			padding: 30rpx;
			box-sizing: border-box;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.searchboxitem {
				width: 620rpx;
				background: #F7F8FA;
				border-radius: 200rpx 200rpx 200rpx 200rpx;
				position: relative;

				.searchtext {
					position: absolute;
					right: 5rpx;
					top: 4.5rpx;
					width: 156rpx;
					height: 60rpx;
					background: #4A9CFF;
					border-radius: 200rpx 200rpx 200rpx 200rpx;
					opacity: 1;
					display: inline-block;
					font-size: 30rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #FFFFFF;
					line-height: 42rpx;
					text-align: center;
					line-height: 60rpx;
					z-index: 999999;
				}
			}

			.quxiao {
				font-size: 32rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #333333;
				line-height: 45rpx;
			}
		}

		.historydata {
			width: 100%;
			padding: 30rpx;
			box-sizing: border-box;

			.historytitle {
				display: flex;
				justify-content: space-between;
				align-items: center;

				.text {
					font-size: 32rpx;
					font-family: PingFang SC-Medium, PingFang SC;
					font-weight: 500;
					color: #000000;
					line-height: 38rpx;
				}

			}

			.del {
				.deleteimg {
					width: 48rpx;
					height: 48rpx;
				}
			}
		}

		.searchContent {
			width: 100%;
			height: 1300rpx;
			background-color: #F7F8FA;

			.searchtabs {
				width: 100%;
				height: 60rpx;

			}

			.searchcontentData {
				width: 100%;
				padding: 30rpx;
				box-sizing: border-box;
				margin-top: 30rpx;
				display: flex;
				justify-content: space-between;
				flex-wrap: wrap;

				.searchcontentDataitem {
					width: 336rpx;
					height: 562rpx;
					background: #FFFFFF;
					box-shadow: 0rpx 0rpx 24rpx 0rpx rgba(206, 220, 237, 0.2);
					border-radius: 30rpx 30rpx 30rpx 30rpx;
					margin-bottom: 20rpx;

					.imgbox {
						width: 336rpx;
						height: 360rpx;

						.tearchtimg {
							width: 100%;
							height: 100%;
							border-radius: 20rpx;
						}

					}

					.messagebox {
						height: 184rpx;
						display: flex;
						flex-direction: column;
						justify-content: space-around;

						.name {
							width: 300rpx;
							white-space: nowrap;
							overflow: hidden;
							text-overflow: ellipsis;

							// background-color: pink;
							font-size: 32rpx;
							font-family: PingFang SC-Bold, PingFang SC;
							font-weight: bold;
							color: #333333;
							line-height: 50rpx;
							margin-left: 20rpx;
						}

						.zhicheng {
							font-size: 26rpx;
							font-family: PingFang SC-Regular, PingFang SC;
							font-weight: 400;
							color: #666666;
							line-height: 40rpx;
							margin-left: 20rpx;
						}

						.zhuanye {
							padding: 0rpx 20rpx;
							display: flex;
							white-space: nowrap;
							width: 100%;
							white-space: nowrap;
							overflow: hidden;
							text-overflow: ellipsis;
							box-sizing: border-box;

							.zhichengitem {
								display: inline-block;
								height: 48rpx;
								background: #F0F7FF;
								border-radius: 12rpx 12rpx 12rpx 12rpx;
								font-size: 24rpx;
								font-family: PingFang SC-Regular, PingFang SC;
								font-weight: 400;
								color: #4A9CFF;
								line-height: 28rpx;
								text-align: center;
								line-height: 48rpx;
								margin-right: 10rpx;
								padding: 0 10rpx;
							}
						}
					}



				}
			}


		}
	}
</style>